package com.mall.manager.power.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.util.WebUtils;

import com.mall.manager.power.service.ManagePowerService;
import com.mall.untils.Page;
/**权限控制
 * @author zhaopeisen
 *
 */
@Controller
public class ManagePowerController {
	
	 Logger logger = LoggerFactory.getLogger(ManagePowerController.class);
	 @Resource ManagePowerService managerpowerService;

	/**
     * 权限列表
     * @param request
     * @param response
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "/manager_power_list") 
    public String manager_power_list(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
        Map<String,Object> paraMap = WebUtils.getParametersStartingWith(request, "");
		int type = MapUtils.getIntValue(paraMap, "type");
    	Page page=managerpowerService.manager_power_list(request);
     	if(null!=page){
	     	modelMap.addAttribute("manager_power_list", (List<Map<String,Object>>)page.getResult());
	     	int totalpagecount = page.getTotalPageCount();
	     	modelMap.addAttribute("totalpagecount", totalpagecount);
	     	int currentpageno =  page.getCurrentPageNo();
	     	modelMap.addAttribute("currentpageno", currentpageno);
	     	int previouspage = page.getCurrentPageNo() <= 1 ? 1 : page
					.getCurrentPageNo() - 1;
	     	modelMap.addAttribute("previouspage", previouspage);
	     	int nextpage = page.getCurrentPageNo() >= page.getTotalPageCount() ? page
					.getTotalPageCount() : page.getCurrentPageNo() + 1;
			modelMap.addAttribute("nextpage", nextpage); 
			List<Integer> showNumList = page.getShowNumList();
			modelMap.addAttribute("pagenumlist", showNumList);
			modelMap.addAttribute("type", type);
     	}
    	return "power/manager_power_list";
    }
    
    /**
     * 权限添加
     * @param request
     * @param response
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "/manager_power_add")
    public String manager_power_add(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
    	return "power/manager_power_add";
    }
    
    /**
     * 保存权限
     * @param request
     * @param response
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "/manager_power_save") 
    public String manager_power_save(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
    	Map<String, Object> paraMap = WebUtils.getParametersStartingWith(request, "");
    	int update=managerpowerService.manager_power_save(paraMap);
    	if(update>0){
    		modelMap.addAttribute("info", "添加成功！");
    	    return "redirect:/manager_power_list "; 
    	}
    	modelMap.addAttribute("info", "添加失败！");
    		return "redirect:/manager_power_list ";
    }
    
    /**
     * 修改权限
     * @param request
     * @param response
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "/manager_power_edit")    
    public String manager_power_edit(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
    	Map<String, Object> paraMap = WebUtils.getParametersStartingWith(request, "");
    	Map<String, Object> power = managerpowerService.manager_power_findone(paraMap);	
    	int currentpageno = MapUtils.getIntValue(paraMap, "currentpageno");
    	modelMap.addAttribute("power", power);
    	modelMap.addAttribute("currentpageno", currentpageno);
    	return "power/manager_power_edit";
    }
    
    /**
     * 更新权限
     * @param request
     * @param response
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "/manager_power_update")    
    public String manager_power_update(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
    	Map<String, Object> paraMap = WebUtils.getParametersStartingWith(request, "");
    	int update=managerpowerService.manager_power_update(paraMap);
    	int currentpageno = MapUtils.getIntValue(paraMap, "currentpageno");
    	if(update>0){
    		modelMap.addAttribute("info", "修改成功！");
    		return "redirect:/manager_power_list?currentpageno="+currentpageno+"&type=1";
    	}
    	modelMap.addAttribute("info", "修改失败！");
		return "redirect:/manager_power_list?currentpageno="+currentpageno;
    }
    
    /**
     * 删除权限
     * @param request
     * @param response
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "/manager_power_delete")    
    public String manager_power_delete(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
    	Map<String, Object> paraMap = WebUtils.getParametersStartingWith(request, "");
    	int update=managerpowerService.manager_power_delete(paraMap);
    	int currentpageno = MapUtils.getIntValue(paraMap, "currentpageno");
    	if(update>0){
    		modelMap.addAttribute("info", "删除成功！");
    		//对是否换页进行判断
    		int pagecounts=managerpowerService.manager_totalPagecounts()<currentpageno?managerpowerService.manager_totalPagecounts():currentpageno;
    	    return "redirect:/manager_power_list?currentpageno="+pagecounts+"&type=2";
    	}else if(update==-1){
    		modelMap.addAttribute("info", "含有子权限，请先删除子权限！");
    	    return "redirect:/manager_power_list?currentpageno="+currentpageno;
    	}
    	modelMap.addAttribute("info", "删除失败！");
    	return "redirect:/manager_power_list?currentpageno="+currentpageno;
    }
    
    /**
     * 所有的本级权限列表
     * @param request
     * @param response
     * @param modelMap
     * @return
     */
    @ResponseBody 
    @RequestMapping(value = "/manager_getallpowers")
    public String manager_getallpowers1(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
    	Map<String, Object> paraMap = WebUtils.getParametersStartingWith(request, "");
    	List<Map<String, Object>> manager_part_lists = managerpowerService.manager_getallpowers1(paraMap);
     	if(null!=manager_part_lists){
     		JSONArray jsonarray = JSONArray.fromObject(manager_part_lists);  
            return jsonarray.toString(); 
     	}
		return null;
    }
}
